package com.pateo.bjev.contacts;

import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import com.pateo.bjev.contacts.adapter.CallItemDecoration;
import com.pateo.bjev.contacts.adapter.SearchNameAdapter;
import com.pateo.bjev.contacts.utils.CallUtils;
import com.pateo.bjev.contacts.utils.ContactSearchUtils;
import com.pateo.bluetooth.logic.bean.ContactData;
import com.pateo.bluetooth.logic.contact.manager.ContactsManager;
import java.util.List;

/**
 * 主页面搜索
 * Created by pateo on 17-5-13.
 */

public class MainSearchPlugin implements TextWatcher,ContactSearchUtils.SearchCallback<ContactData>{

    MainActivity activity;

    ViewPager mainPager;
    View noConnectedView;
    View searchResultView;

    RecyclerView searchNameList;
    EditText input_name_edit;

    ContactsManager contactsManager;
    ContactSearchUtils<ContactData> searchUtils;
    SearchNameAdapter adapter;

    public MainSearchPlugin(MainActivity activity){
        this.activity = activity;

        mainPager = (ViewPager) activity.findViewById(R.id.pager);
        noConnectedView = activity.findViewById(R.id.bt_no_connect);
        searchResultView = activity.findViewById(R.id.search_name_layout);

        searchNameList = (RecyclerView) activity.findViewById(R.id.search_name_result_list);
        input_name_edit = (EditText) activity.findViewById(R.id.input_name_edit);
        input_name_edit.addTextChangedListener(this);

        contactsManager = ContactsManager.getInstance(activity.getApplicationContext());
        searchUtils = new ContactSearchUtils.SearchNameUtils(activity);

        adapter = new SearchNameAdapter(activity,null);
        searchNameList.setLayoutManager(new LinearLayoutManager(activity,LinearLayoutManager.VERTICAL,false));
        searchNameList.addItemDecoration(new CallItemDecoration(activity,LinearLayoutManager.HORIZONTAL,2, Color.WHITE));
        searchNameList.setAdapter(adapter);
    }

    public void active(boolean enable){
        if(!enable){
            mainPager.setVisibility(View.VISIBLE);
            searchResultView.setVisibility(View.GONE);
            noConnectedView.setVisibility(View.GONE);
            return;
        }
        if(!contactsManager.isBluetoothConnect()){
            mainPager.setVisibility(View.GONE);
            searchResultView.setVisibility(View.GONE);
            noConnectedView.setVisibility(View.VISIBLE);
        }else{
            mainPager.setVisibility(View.GONE);
            searchResultView.setVisibility(View.VISIBLE);
            noConnectedView.setVisibility(View.GONE);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(TextUtils.isEmpty(charSequence)){
            adapter.updateContactList(null);
            return;
        }
        searchUtils.searchContacts(charSequence.toString(),this);
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    @Override
    public void onSearchResult(List<ContactData> list) {
        adapter.updateContactList(list);
    }

    @Override
    public void onSearchOne(ContactData data) {

    }
}
